/* Test2.cs -- 
   Copyright (C) 2008  Casey Marshall.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


using System;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using NUnit.Framework;

using SexpSharp.Remoting;

namespace SexpSharp.Remoting.Tests
{
    public interface IHello2
    {
		[SexpMethodName("hello")]
        string HelloWorld();

        void CheckProtocolVersion(string s1, string s2, int v);
        Blob HelloBlob();
    }

    public class Hello2 : MarshalByRefObject, IHello2
    {
        const int protocol = 5;
		[SexpMethodName("hello")]
        public string HelloWorld()
        {
            return "Hello, World!";
        }

        public void CheckProtocolVersion(string s1, string s2, int v)
        {
            if (v != protocol)
                throw new Exception("incompatible protocol");
        }

        Blob blob = new Blob("Hello, World!");

        public Blob HelloBlob()
        {
            return blob;
        }
    }

    public class Blob : MarshalByRefObject
    {
        public DateTime dateTime;
        string blob;
        public Blob(string b) { blob = b; dateTime = DateTime.Now; }
        public string GetBlob() { return blob; }
    }

    [TestFixture]
    public class Test2
    {
        object[] foo = new object[4];

        [Test]
        public void a()
        {
            setupRemotingChannel();
            registerService();
            DateTime begin = DateTime.Now;
            for (int i = 0; i < 10000; i++)
                runRemote();
            DateTime end = DateTime.Now;
            TimeSpan elapsed = end - begin;
            Console.Error.WriteLine(elapsed + " -- " + (elapsed.TotalMilliseconds / 10000));
        }

        void setupRemotingChannel()
        {
            Hashtable props = new Hashtable();
            props["port"] = 6001;
			props["name"] = "RemotingTest2";
            SexpServerFormatterSinkProvider serv =
                new SexpServerFormatterSinkProvider();
            SexpClientFormatterSinkProvider cli =
                new SexpClientFormatterSinkProvider();
            TcpChannel channel = new TcpChannel(props, cli, serv);
            ChannelServices.RegisterChannel(channel, false);
        }

        void registerService()
        {
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello2),
                                                               "hello2",
                                                               WellKnownObjectMode.Singleton);
        }

        void runRemote()
        {
            IHello2 hello = (IHello2) Activator.GetObject(typeof(IHello2),
                                                          "tcp://localhost:6001/hello2");
            foo[0] = hello.HelloWorld();
            hello.CheckProtocolVersion(null, null, 5);
            foo[1] = hello.HelloBlob();
            foo[2] = hello.HelloBlob().GetBlob();
            foo[3] = hello.HelloBlob().dateTime;
        }
    }
}